package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author atguigu-mqx
 */
@Controller
public class ActivityController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    //  http://activity.gmall.com/seckill.html
    //  秒杀列表!
    @GetMapping("seckill.html")
    public String seckillList(Model model){
        //  存储一个集合
        Result result = activityFeignClient.findAll();
        //  存储数据
        model.addAttribute("list",result.getData());
        //  返回秒杀列表页面！
        return "seckill/index";
    }

    //  th:href="'/seckill/'+${item.skuId}+'.html'"
    //  秒杀详情控制器
    @GetMapping("/seckill/{skuId}.html")
    public String seckillItem(@PathVariable Long skuId,Model model){
        //  获取到秒杀详情：
        Result result = this.activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item",result.getData());
        //  返回秒杀详情页面
        return "seckill/item";
    }

    //  http://activity.gmall.com/seckill/queue.html?skuId=22&skuIdStr=c81e728d9d4c2f636f067f89cc14862c
    //  排队页面控制器！
    @GetMapping("seckill/queue.html")
    public String queue(HttpServletRequest request){
        //  show默认值 1       ${skuId}        ${skuIdStr}
        request.setAttribute("skuId",request.getParameter("skuId"));
        request.setAttribute("skuIdStr",request.getParameter("skuIdStr"));
        //  返回排队页面！
        return "seckill/queue";
    }

    //  		<a href="/seckill/trade.html" target="_blank">去下单</a>
    @GetMapping("/seckill/trade.html")
    public String seckillTrade(Model model){
        //  远程调用  获取数据
        Result<Map> result = activityFeignClient.trade();
        if (result.isOk()){
            model.addAllAttributes(result.getData());
            //  返回订单页面.
            return "seckill/trade";
        }else {
            model.addAttribute("message","去下单失败.");
            return "seckill/fail";
        }

    }


}
